package com.jhclothing.product.service.impl;

import com.jhclothing.common.constant.UserConstants;
import com.jhclothing.common.enums.ClothingSex;
import com.jhclothing.common.utils.StringUtils;
import com.jhclothing.product.domain.ClothingInventory;
import com.jhclothing.product.domain.ClothingType;
import com.jhclothing.product.mapper.ClothingInventoryMapper;
import com.jhclothing.product.service.IClothingInventoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 服饰库存Service业务层处理
 *
 * @author zyp
 * @date 2024-04-24
 */
@Service
public class ClothingInventoryServiceImpl implements IClothingInventoryService {
    @Autowired
    private ClothingInventoryMapper clothingInventoryMapper;

    /**
     * 查询服饰库存
     *
     * @param clothingInventoryId 服饰库存主键
     * @return 服饰库存
     */
    @Override
    public ClothingInventory selectClothingInventoryByClothingInventoryId(Long clothingInventoryId) {
        ClothingInventory clothingInventory = clothingInventoryMapper.selectClothingInventoryByClothingInventoryId(clothingInventoryId);
        if (null != clothingInventory) {
            ClothingType clothingType = setClothingTypeCodeAndSex(clothingInventory);
            clothingInventory.setClothingType(clothingType);
        }
        return clothingInventory;
    }

    /**
     * 查询服饰库存列表
     *
     * @param clothingInventory 服饰库存
     * @return 服饰库存
     */
    @Override
    public List<ClothingInventory> selectClothingInventoryList(ClothingInventory clothingInventory) {
        return clothingInventoryMapper.selectClothingInventoryList(clothingInventory);
    }

    /**
     * 查询服饰品类ID+服饰尺码的产品是否唯一
     *
     * @param clothingInventory 服饰库存信息
     * @return true，唯一   false，不唯一
     */
    @Override
    public boolean checkClothingTypeAndSizeUnique(ClothingInventory clothingInventory) {
        Long clothingInventoryId = (null == clothingInventory.getClothingInventoryId() ? -1L : clothingInventory.getClothingInventoryId());
        ClothingInventory inventory = clothingInventoryMapper.checkClothingTypeAndSizeUnique(clothingInventory.getClothingTypeId(), clothingInventory.getClothingSize());
        if ((null != inventory) && inventory.getClothingInventoryId().longValue() != clothingInventoryId) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 根据服饰品类ID + 服饰尺码查询服饰库存
     * @param clothingTypeId 服饰品类ID
     * @param clothingSize 服饰尺码
     * @return 服饰库存
     */
    public ClothingInventory selectClothingInventoryByClothingTypeAndSize(Long clothingTypeId, String clothingSize) {
        ClothingInventory inventory = clothingInventoryMapper.checkClothingTypeAndSizeUnique(clothingTypeId, clothingSize);
        if (null != inventory) {
            ClothingType clothingType = setClothingTypeCodeAndSex(inventory);
            inventory.setClothingType(clothingType);
        }

        return inventory;
    }

    /**
     * 新增服饰库存
     *
     * @param clothingInventory 服饰库存
     * @return 结果
     */
    @Override
    public int insertClothingInventory(ClothingInventory clothingInventory) {
        return clothingInventoryMapper.insertClothingInventory(clothingInventory);
    }

    /**
     * 修改服饰库存
     *
     * @param clothingInventory 服饰库存
     * @return 结果
     */
    @Override
    public int updateClothingInventory(ClothingInventory clothingInventory) {
        return clothingInventoryMapper.updateClothingInventory(clothingInventory);
    }

    /**
     * 更新服饰库存
     *
     * @param clothingInventoryId 服饰库存ID
     * @param nowInventory        服饰库存
     * @param updateBy            更新者
     * @return 结果
     */
    @Override
    public int updateClothingInventory(Long clothingInventoryId, Integer nowInventory, String updateBy) {
        return clothingInventoryMapper.updateClothingInventoryInfo(clothingInventoryId, nowInventory, updateBy);
    }

    /**
     * 批量删除服饰库存
     *
     * @param clothingInventoryIds 需要删除的服饰库存主键
     * @return 结果
     */
    @Override
    public int deleteClothingInventoryByClothingInventoryIds(Long[] clothingInventoryIds) {
        return clothingInventoryMapper.deleteClothingInventoryByClothingInventoryIds(clothingInventoryIds);
    }

    /**
     * 删除服饰库存信息
     *
     * @param clothingInventoryId 服饰库存主键
     * @return 结果
     */
    @Override
    public int deleteClothingInventoryByClothingInventoryId(Long clothingInventoryId) {
        return clothingInventoryMapper.deleteClothingInventoryByClothingInventoryId(clothingInventoryId);
    }

    private ClothingType setClothingTypeCodeAndSex(ClothingInventory clothingInventory) {
        ClothingType clothingType = clothingInventory.getClothingType();
        String clothingSexName = ClothingSex.getClothingSexNameByCode(clothingType.getClothingSex());
        if (StringUtils.isNotEmpty(clothingSexName)) {
            clothingType.setClothingTypeCodeAndSex(clothingType.getClothingTypeCode() + " - " + clothingSexName);
        } else {
            clothingType.setClothingTypeCodeAndSex(clothingType.getClothingTypeCode());
        }
        return clothingType;
    }
}
